package com.javaweb.dao.impl;

import com.javaweb.dao.TagDao;
import com.javaweb.model.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

@Repository
public class TagDaoImpl implements TagDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Tag tag) {
        if (ObjectUtils.isEmpty(tag) || ObjectUtils.isEmpty(tag.getName())) {
            return 0; // Insert failed
        }

        String sql = "INSERT INTO tb_tag (name, create_time) VALUES (?, ?)";
        Object[] args = new Object[]{
                tag.getName(),
                new java.sql.Date(tag.getCreateTime().getTime())
        };

        return jdbcTemplate.update(sql, args) > 0 ? 1 : 0; // Return insert result
    }

    @Override
    public int insertArticleTag(Integer articleId, Integer tagId) {
        if (ObjectUtils.isEmpty(articleId) || ObjectUtils.isEmpty(tagId)) {
            return 0; // Association failed
        }

        String sql = "INSERT INTO tb_article_tag (article_id, tag_id) VALUES (?, ?)";
        return jdbcTemplate.update(sql, articleId, tagId) > 0 ? 1 : 0; // Return association result
    }
}
